package com.ujoku.controller.admin;

import com.labillusion.core.platform.crypto.MD5;
import com.labillusion.core.platform.exception.UserAuthorizationException;
import com.labillusion.core.platform.web.pagination.PaginationForm;
import com.labillusion.core.platform.web.pagination.PaginationView;
import com.labillusion.core.platform.web.rest.RESTController;
import com.labillusion.core.platform.web.rest.exception.InvalidRequestException;
import com.labillusion.core.util.StringUtils;
import com.ujoku.context.constants.MessageConstants;
import com.ujoku.domain.Message;
import com.ujoku.domain.MyListItem;
import com.ujoku.domain.RepurchaseItem;
import com.ujoku.domain.RepurchaseStatus;
import com.ujoku.domain.admin.User;
import com.ujoku.request.admin.LoginForm;
import com.ujoku.request.admin.RepurchaseForm;
import com.ujoku.request.admin.RepurchaseSearchForm;
import com.ujoku.service.MessageService;
import com.ujoku.service.MyListItemService;
import com.ujoku.service.MyListService;
import com.ujoku.service.RepurchaseItemService;
import com.ujoku.service.admin.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 14-11-1.
 */
@RestController
public class RepurchaseMsgController extends RESTController {

    @Autowired
    private RepurchaseItemService repurchaseItemService;

    @Autowired
    private MessageService messageService;

    @RequestMapping(value="/uj-admin/repurchase/list", method = RequestMethod.POST)
    public PaginationView<RepurchaseItem> parametersList(@RequestBody @Valid RepurchaseSearchForm form){

        Map<String,Object> query = form.getQueryMap();
        if(StringUtils.hasText(form.getMemberName())){
            query.put("memberName", form.getMemberName());
        }

        if(form.getStatus() != null){
            query.put("status", form.getStatus());
        }

        PaginationView<RepurchaseItem> view = new PaginationView<RepurchaseItem>();
        view.setList(repurchaseItemService.getListForAdmin(query));
        view.setCount(repurchaseItemService.getListCountForAdmin(query));

        return view;
    }

    @RequestMapping(value="/uj-admin/repurchase/{id}", method = RequestMethod.GET)
    public RepurchaseItem parametersList(@PathVariable String id){

        return repurchaseItemService.getItemForAdmin(id);
    }

    //设置回购价格
    @RequestMapping(value="/uj-admin/repurchase", method = RequestMethod.PUT)
    public String setPrice(@RequestBody @Valid RepurchaseForm form){
        RepurchaseItem repurchaseItem = repurchaseItemService.getItemForAdmin(form.getId());
        if(repurchaseItem == null || repurchaseItem.getStatus() >  RepurchaseStatus.PRICE_CONFIRM)
            throw new InvalidRequestException("can not found repurchase item");

        repurchaseItem.setPrice(form.getPrice());
        repurchaseItem.setStatus(RepurchaseStatus.PRICE_CONFIRM);

        Message message = messageService.bulid(MessageConstants.REPURCHASE, repurchaseItem.getMemberId(), null);
        messageService.insert(message);

        repurchaseItemService.update(repurchaseItem);

        return "success";
    }


}
